﻿using Echars.Common.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using WF_Business;

namespace Echars.Common.BLL
{
    public class EcharsBLL
    {
        public static EcharsBLL Instance { get { return new EcharsBLL(); } }
        public string GetBuildingName(string id)
        {
            var strSql = string.Format("select 地址||case when regexp_instr(幢号,'[幢|栋|#]')>0 then 幢号  else 幢号||'幢' end 幢号 from at_zhuangbasic where id='{0}'", id);
            return SysParams.OAConnection().GetValue(strSql);
        }

        public string GetFloorNo(string zid)
        {
            var strSql = string.Format("select max(楼层号) from at_loupan where zid='{0}'", zid);
            return SysParams.OAConnection().GetValue(strSql);
        }

        private DataTable GetUnitNo(string zid)
        {
            var strSql = string.Format(@"select 单元 from at_loupan where zid='{0}'
                                        and 楼层号=(select min(楼层号) from at_loupan where zid='{0}')
                                        order by 楼层号,房间号,translate(单元,'一二三四五六七','1234567') ", zid);
            DataTable dtTemp;
            SysParams.OAConnection().RunSql(strSql, out dtTemp);
            return dtTemp;
        }

        private DataTable GetHouseInfo(string zid)
        {
            var strSql = string.Format(@"select id,楼层号,房间号,楼层房间号,预测套内面积,预测建筑面积,预测分摊面积 from at_loupan where zid='{0}' order by 楼层号,房间号", zid);
            DataTable dtTemp;
            SysParams.OAConnection().RunSql(strSql, out dtTemp);
            return dtTemp;
        }

        public List<string> GetUnitNoList(string zid)
        {
            var dtTemp = GetUnitNo(zid);
            var list = dtTemp.AsEnumerable().Select(t => t.Field<string>("单元") ?? "").ToList();
            return list;
        }

        public IEnumerable<HouseInfo> GetHouseInfoList(string zid)
        {
            var dtTemp = GetHouseInfo(zid);
            var list = dtTemp.AsEnumerable().Select(t => new HouseInfo
            {
                ID = t.Field<string>("ID"),
                FloorNo = t["楼层号"].ToString(),
                HouseNo = t["房间号"].ToString(),
                FloorHouseNo = t.Field<string>("楼层房间号"),
                PredictedInnerArea = t.Field<string>("预测套内面积") ?? "",
                PredictedBuildingArea = t.Field<string>("预测建筑面积") ?? "",
                PredictedShareArea = t.Field<string>("预测分摊面积") ?? ""
            });
            return list;
        }
    }
}
